---
title: "Japan 2021 fait-accompli experiment"
author: "Viet Hung Nguyen Cao"
date: "2024-08-08"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Packages:

```{r package, include=TRUE,echo=FALSE}
library(tidyverse)
library(tabbycat)
library(stargazer)
library(jtools)
library(gtsummary)
library(multcomp)
library(ggpubr)
library(rstatix)
library(xtable)
library(modelsummary)
library(here)
```

# Loading data:

```{r data, include=TRUE,echo=FALSE}
JP_data <- read_csv("1206_Jpn_data.csv")
names(JP_data)
str(JP_data)
JP_data <- JP_data %>% slice(-c(1,2)) %>%
  dplyr::select(
    IPAddress,
    'Duration (in seconds)',
    LocationLatitude,
    LocationLongitude,
    UserLanguage,
    Q1,
    Q2,
    Q3,
    Q4,
    Q5,
    Q6,
    Q7_1,
    Q8,
    Q9,
    Q10,
    Q11,
    Q12_4,
    Q13,
    Q14,
    Q15,
    Q18,
    Q21,
    Q24,
    Q27,
    Q30,
    Q33,
    Q35,
    Q36,
    Q37,
    Q38,
    Q39,
    Q40
  ) %>%
  rename(
    consent = Q1,
    age = Q2,
    gender = Q3,
    education = Q4,
    income = Q5,
    region = Q6,
    polbelief = Q7_1,
    polparty = Q8,
    news = Q9,
    threat = Q10,
    pretreat = Q11,
    institutiontrust = Q12_4,
    nationalistic1 = Q13,
    nationalistic2 = Q14,
    nationalistic3 = Q15,
    control = Q18,
    warthreat = Q21,
    weakpower = Q24,
    econsanction = Q27,
    court = Q30,
    UNmediation = Q33,
    check = Q35,
    buildbase = Q36,
    posttreat = Q37,
    missile = Q38,
    alliance = Q39,
    nuclear = Q40
  )
str(JP_data$nationalistic1)
str(JP_data$nationalistic2)
str(JP_data$age)
str(JP_data$gender)
str(JP_data$education)
str(JP_data$income)
str(JP_data$polbelief)
```

# Recode variables:

```{r recode, include=TRUE,echo=FALSE}
JP_data$polbelief <- as.numeric(JP_data$polbelief)
str(JP_data$institutiontrust)
JP_data$institutiontrust <- as.numeric(JP_data$institutiontrust)
JP_data <- JP_data %>% 
  mutate(
    age = case_when(
      age == "20-29"~1,
      age == "30-39"~2,
      age == "40-49"~3,
      age == "50-59"~4,
      age == "60+"~5
    ),
    women = case_when(
      gender == "Female"~1,
      gender != "Female"~0
    ),
    education = case_when(
      education == "Primary school graduate"~1,
      education == "Junior high school graduate"~2,
      education == "High school graduate"~3,
      education == "Vocational school graduate"~4,
      education == "College graduate"~5,
      education == "Master's degree"~6,
      education == "Doctoral degree"~7
    ),
    college = ifelse(
      education %in% c(5,6,7),1,0
    ),
    income = case_when(
      income == "Less than ¥150,000"~1,
      income == "¥150,000 to ¥300,000"~2,
      income == "¥300,000 to ¥500,000"~3,
      income == "¥500,000 to ¥600,000"~4,
      income == "¥600,000 to ¥800,000"~5,
      income == "¥800,000 to ¥1,000,000"~6,
      income == "¥1,000,000 to ¥1,200,000"~7,
      income == "¥1,200,000 or more"~8),
  )
JP_data <- JP_data %>% 
  mutate(
    news = case_when(
      news == "Not closely at all"~1,
      news == "Not so closely"~2,
      news == "Somewhat closely"~3,
      news == "Very closely"~4
    ),
    nationalistic1 = case_when(
      nationalistic1 == "Not at all well"~1,
      nationalistic1 == "Not too well"~2,
      nationalistic1 == "Somewhat well"~3,
      nationalistic1 == "Very well"~4,
      nationalistic1 == "Extremely well"~5
    ),
    nationalistic2 = case_when(
      nationalistic2 == "Not at all"~1,
      nationalistic2 == "Not too much"~2,
      nationalistic2 == "Somewhat"~3,
      nationalistic2 == "A great deal"~4
    ),
    nationalistic3 = case_when(
      nationalistic3 == "Never"~1,
      nationalistic3 == "Not too often"~2,
      nationalistic3 == "Sometimes"~3,
      nationalistic3 == "Most of the time"~4,
      nationalistic3 == "Always"~5
    )
  )
JP_data <- JP_data %>%
  mutate(approvalrate = coalesce(control, warthreat, weakpower,
                                 econsanction, court, UNmediation),
         approvalrate = case_when(
           approvalrate == "Neither approve or disapprove"~0,
           approvalrate == "Strongly disapprove"~-2,
           approvalrate == "Somewhat disapprove"~-1,
           approvalrate == "Somewhat approve"~1,
           approvalrate == "Strongly approve"~2
         ),
         approvalrate=as.numeric(approvalrate),
         control_grp = case_when(control != "NA" ~ 1,
                                 control == "NA" ~ 0),
         control_grp = as.factor(control_grp),
         warthreat_grp = case_when(warthreat != "NA" ~ 1,
                                   warthreat == "NA" ~ 0),
         warthreat_grp = as.factor(warthreat_grp),
         weakpower_grp = case_when(weakpower != "NA" ~ 1,
                                   weakpower == "NA" ~ 0),
         weakpower_grp = as.factor(weakpower_grp),
         econsanction_grp = case_when(econsanction != "NA" ~ 1,
                                      econsanction == "NA" ~ 0),
         econsanction_grp = as.factor(econsanction_grp),
         court_grp = case_when(court != "NA" ~ 1,
                               court == "NA" ~ 0),
         court_grp = as.factor(court_grp),
         UNmediation_grp = case_when(UNmediation != "NA" ~ 1,
                                     UNmediation == "NA" ~ 0),
         UNmediation_grp = as.factor(UNmediation_grp))
JP_data <- JP_data %>% 
  mutate(
    pretreat_GDP = case_when(
      pretreat == "Strongly oppose" ~ 1,
      pretreat == "Somewhat oppose" ~ 2,
      pretreat == "Somewhat support" ~ 3,
      pretreat == "Strongly support" ~ 4
    ),
    posttreat_GDP = case_when(
      posttreat == "Strongly oppose" ~ 1,
      posttreat == "Somewhat oppose" ~ 2,
      posttreat == "Somewhat support" ~ 3,
      posttreat == "Strongly support" ~ 4
    ),
    buildbase_support = case_when(
      buildbase == "Strongly oppose" ~ 1,
      buildbase == "Somewhat oppose" ~ 2,
      buildbase == "Somewhat support" ~ 3,
      buildbase == "Strongly support" ~ 4
    ),
    alliance_support = case_when(
      alliance == "Strongly oppose" ~ 1,
      alliance == "Somewhat oppose" ~ 2,
      alliance == "Somewhat support" ~ 3,
      alliance == "Strongly support" ~ 4
    ),
    missile_support = case_when(
      missile == "Strongly oppose" ~ 1,
      missile == "Somewhat oppose" ~ 2,
      missile == "Somewhat support" ~ 3,
      missile == "Strongly support" ~ 4
    ),
    nuclear_support = case_when(
      nuclear == "Strongly oppose" ~ 1,
      nuclear == "Somewhat oppose" ~ 2,
      nuclear == "Somewhat support" ~ 3,
      nuclear == "Strongly support" ~ 4
    )
  )
JP_data <- JP_data %>%
  mutate(
    check_condition = case_when(check == "China" ~ 1,
                                check != "China" ~ 0),
    check_condition = as.factor(check_condition)
  ) %>% filter(consent != "I do not consent, I do not wish to participate",
               check_condition == "1")
```

# Wrangling:

```{r wrangling, include=TRUE,echo=FALSE}
JP_data <- JP_data %>% 
  mutate(
    age = factor(age, levels = c("1","2","3","4","5")),
    education = factor(education, levels = c("1","2","3","4","5","6","7")),
    income = factor(income, levels = c("1","2","3","4","5","6","7","8")),
    news = factor(news, levels = c("1","2","3","4")),
    nationalistic = (as.numeric(nationalistic1)+as.numeric(nationalistic2)+as.numeric(nationalistic3))/3
  ) %>% mutate(group = case_when(control_grp == 1 ~ "control",
                                               warthreat_grp == 1~"warthreat",
                                               weakpower_grp == 1~"weakpower",
                                               econsanction_grp == 1~"econsanction",
                                               court_grp == 1~"court",
                                               UNmediation_grp == 1~"UNmediation"
                                               ),
               group=as.factor(group)) %>%
    mutate(
      bd_support=ifelse(approvalrate %in% c(1,2),1,0)
    )
```

# Analysis:
## Diff-in-means
```{r analysis, include=TRUE,echo=TRUE}
mean1 <- JP_data %>% 
  group_by(group) %>% 
  summarise(
    mean = mean(approvalrate,na.rm=T),
    var = var(approvalrate,na.rm=T),
    n=sum(!is.na(approvalrate)))
print(mean1)
```

## Two sample t-tests
```{r t-test, include=TRUE,echo=TRUE}
t.test(JP_data$approvalrate[JP_data$group == "control"], 
       JP_data$approvalrate[JP_data$group == "warthreat"])
t.test(JP_data$approvalrate[JP_data$group == "control"], 
       JP_data$approvalrate[JP_data$group == "weakpower"])
t.test(JP_data$approvalrate[JP_data$group == "control"], 
       JP_data$approvalrate[JP_data$group == "econsanction"])
t.test(JP_data$approvalrate[JP_data$group == "control"], 
       JP_data$approvalrate[JP_data$group == "court"])
t.test(JP_data$approvalrate[JP_data$group == "control"], 
       JP_data$approvalrate[JP_data$group == "UNmediation"])
```

## Logit
```{r logit,include=TRUE,echo=TRUE}
model1 <- glm(data = JP_data,
              bd_support ~ group,
              family = binomial(link = "logit"))
model2 <- glm(
  data = JP_data,
  bd_support ~ group + age + women + nationalistic + college +
    polbelief + institutiontrust,
  family = binomial(link = "logit")
)
summary(model1)
summary(model2)
```
## Anova
```{r anova,include=TRUE,echo=TRUE}
aov(approvalrate~group,data = JP_data)
```

## OLS
```{r ols, include=TRUE,echo=TRUE}
model3 <- lm(data = JP_data,approvalrate~group)
model4 <- lm(
  data = JP_data,
  approvalrate ~ group + age+ women + gender + nationalistic + college + polbelief +
    institutiontrust
)
summary(model3)
summary(model4)
```

# Balance check:
```{r balance}
bal.test <- lm(
  data = JP_data,
  as.numeric(group) ~ age + gender + nationalistic + education + income +
    polbelief + institutiontrust
)
summary(bal.test)
BalanceR::BalanceR(data=JP_data_cleaned, group = group,
                   cov = c(age, women, education, income)) %>% 
  plot(point.size = 5, text.size = 18)
ggsave("BalanceCheck.png",width = 15,height = 10,units = "in")
```

# Table:
```{r table}
var.names <- 
  c(`groupcourt`= "Court",
    `groupeconsanction`= "Economic Costs",
    `groupUNmediation` = "UN Mediation",
    `groupwarthreat` = "War Threat",
    `groupweakpower` = "Casualty",
    age2 = "30-39",
    age3 = "40-49",
    age4 = "50-59",
    age5 = "60+",
    women = "Gender (female or not)",
    college = "College degree",
    polbelief = "Political Belief",
    institutiontrust = "Trust in Itl. Institution",
    nationalistic = "Nationalist sentiment"
  )
modelsummary(models = list(`Logit` = model1, 
                           `Logit` = model2,
                           `OLS` = model3, 
                           `OLS` = model4),
             gof_map=c("nobs"),
             coef_map=var.names,
             output="latex",
             stars=c('*'=.05),align='ldddd',
             title = "Table 1")
?modelsummary
```

# Graph
```{r graph}
JP_data %>% 
  count(group,bd_support) %>% 
  group_by(group) %>% 
  mutate(prop=n/sum(n)) %>% 
  filter(bd_support == 1) %>%
  ggplot(aes(x=group, y=prop,fill = as.factor(bd_support)))+
  geom_col(position = position_dodge())+
  geom_text(aes(label = round(100*prop)),
            position = position_dodge(.9),vjust=-.2)+
  xlab("Groups")+
  ylab("Percentage of respondents")+
  scale_x_discrete(
    limits = c("control","warthreat","weakpower","econsanction","court","UNmediation"),
    labels = c(
      "control"="Control",
      "warthreat"="War Threat",
      "weakpower"="Casualties",
      "econsanction"="Economic Costs",
      "court"="Intl. Courts",
      "UNmediation"="UN Mediation"
    )
  )+
  scale_fill_grey(guide="none")+
  scale_y_continuous(labels = scales::percent)+
  theme_bw()+
  ggtitle("Support for backing down")
ggsave(here("graphs","Approval percentage.jpeg"),width = 8,height = 5,units = "in")
```

